<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="戏剧名称"
                    prop="name">
        <el-input v-model="dataForm.name"
                  placeholder="戏剧名称"></el-input>
      </el-form-item>
      <el-form-item label="主演"
                    prop="starring">
        <el-input v-model="dataForm.starring"
                  placeholder="主演"></el-input>
      </el-form-item>
      <el-form-item label="片长"
                    prop="length">
        <el-input v-model="dataForm.length"
                  placeholder="片长"></el-input>
      </el-form-item>
      <el-form-item label="观影日期"
                    prop="viewDate">
        <el-date-picker v-model="dataForm.viewDate"
                        type="datetime"
                        placeholder="选择观影日期时间"
                        clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="入场须知"
                    prop="notice">
        <el-input v-model="dataForm.notice"
                  placeholder="入场须知"></el-input>
      </el-form-item>

      <el-form-item label="状态"
                    prop="status">
        <el-switch v-model="dataForm.status"
                   active-color="#13ce66"
                   inactive-color="#ff4949"></el-switch>
      </el-form-item>
      <el-form-item label="备注"
                    prop="remark">
        <el-input v-model="dataForm.remark"
                  placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        starring: '',
        length: '',
        viewDate: '',
        notice: '',
        remark: '',
        status: true,
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: ''
      },
      dataRule: {
        name: [
          { required: true, message: '戏剧名称不能为空', trigger: 'blur' }
        ],
        viewDate: [
          { required: true, message: '观影日期不能为空', trigger: 'blur' }
        ],
        notice: [
          { required: true, message: '入场须知不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态，0：下线；1：正常不能为空', trigger: 'blur' }
        ],
        createdBy: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updatedBy: [
          { required: true, message: '更新人不能为空', trigger: 'blur' }
        ],
        updatedTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/member/drama/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.drama.name
              this.dataForm.starring = data.drama.starring
              this.dataForm.length = data.drama.length
              this.dataForm.viewDate = new Date(data.drama.viewDate * 1000)
              this.dataForm.notice = data.drama.notice
              this.dataForm.remark = data.drama.remark
              this.dataForm.status = data.drama.status === 1
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/member/drama/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'starring': this.dataForm.starring,
              'length': this.dataForm.length,
              'viewDate': this.dataForm.viewDate ? this.dataForm.viewDate.getTime() / 1000 : '',
              'notice': this.dataForm.notice,
              'remark': this.dataForm.remark,
              'status': this.dataForm.status ? 1 : 0,
              'createdBy': this.$store.state.user.name
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
